$(function () {
    // 初始加载学生列表
    loadStudentList();

    // 点击按钮进行搜索
    $("#searchButton").click(function () {
        // 获取搜索关键字
        var searchText = $("#searchName").val();

        // 发起搜索请求并更新页面显示
        searchStudent(searchText);
    });
});

// 加载学生列表函数
function loadStudentList() {
    $.ajax({
        url: "/webapi/student/list",
        type: "GET",
        success: function (data) {
            var html = "";
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                html += "<tr>";
                html += "<td>" + item.id + "</td>";
                html += "<td>" + item.no + "</td>";
                html += "<td>" + item.name + "</td>";
                html += "<td>" + item.password + "</td>";
                html += "<td>" + item.sex + "</td>";
                html += "<td>" + item.age + "</td>";
                html += "<td>" + item.score + "</td>";
                html += "</tr>";
            }
            $("#students").html(html);
            console.log(data);
        }
    });
}

// 搜索学生函数
function searchStudent(keyword) {
    $.ajax({
        url: "/webapi/student/list",
        type: "GET",
        success: function (data) {
            var html = "";
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                if (item.name === keyword) {
                    html += "<tr>";
                    html += "<td>" + item.id + "</td>";
                    html += "<td>" + item.no + "</td>";
                    html += "<td>" + item.name + "</td>";
                    html += "<td>" + item.password + "</td>";
                    html += "<td>" + item.sex + "</td>";
                    html += "<td>" + item.age + "</td>";
                    html += "<td>" + item.score + "</td>";
                    html += "</tr>";
                }
            }
            $("#students").html(html);
            console.log(data);
        }
    });
}
